// pages/common/index.js
const app = getApp();
const {
  Http
} = require("../../utils/http")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scene: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {

    app.login((res) => { //页面初始化
      wx.setStorageSync('register', res.register);
      wx.setStorageSync('shoplist', res.store_list);
      wx.setStorageSync('verifyman', res.verification);
      wx.setStorageSync('distributor', res.distributor);
      wx.setStorageSync('user_id', res.user_id);
      wx.setStorageSync('store_id', res.store_id);
      wx.setStorageSync('money', res.userinfo.money);
      wx.setStorageSync('payment_password', res.userinfo.payment_password);
      wx.hideLoading();
      console.log('成功');
      if (Object.prototype.toString.call(options.scene) !== '[object Undefined]') { //空
        this.setData({
          scene: options.scene
        })
        this.onSetverify();
      }

    });

  },
  onSetverify: async function () {
    let args = decodeURIComponent(this.data.scene).split(',')
    let register = wx.getStorageSync('register')
    console.log(register);

    if (args[0] == "user-1") { //注册核销员
  
      if (register == 1) {
        // 设置核销员
        let result = await Http.request({ //
          url: 'wechat/basics/set_user_verify_auth',
          method: "POST"
        });
        console.log(result);
        if (result.code == 0) {
          //提示跳转
          wx.showToast({
            title: result.msg,
            icon: 'success',
            duration: 3000
          });
          setTimeout(function () {
            wx.switchTab({
              url: '/pages/home/index'
            })
          }, 3000)
        } 
      } else {
        wx.showModal({
          title: '提示',
          content: "您还未注册，是否先注册",
          success: function (res) {
            if (res.cancel) {} else {
              wx.navigateTo({
                url: '/pages/logonzc/index',
              })
            }
          }
        });
      }

    }else{

      if(register==1){
        if (args[0] == "user-2") {
          // 设置分销员
          let result = await Http.request({ //
            url: 'wechat/basics/set_user_distributor_auth',
            method: "POST"
          });
          console.log(result);
          if (result.code == 0) {
            //提示跳转
            wx.showToast({
              title: result.msg,
              icon: 'success',
              duration: 3000
            });
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/home/index'
              })
            }, 3000)
          } 

        }
      }else{
        wx.showModal({
          title: '提示',
          content: "您还未注册，是否先注册",
          success: function (res) {
            if (res.cancel) {
              //点击取消,默认隐藏弹框
            } else {
              //console.log("点击确定");
              //点击确定
              wx.navigateTo({
                url: '/pages/logonfx/index',
              })
            }
          }
        });

      }
      

    }
    
  }

})